package com.tianji.learning.mq;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.course.CourseFullInfoDTO;
import com.tianji.api.dto.course.CourseSimpleInfoDTO;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.service.ILearningLessonService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.tianji.common.constants.MqConstants.Queue.*;
import static com.tianji.common.constants.MqConstants.Exchange.*;
import static com.tianji.common.constants.MqConstants.Key.*;

/**
 * @Author: Yang
 * @CreateTime:
 * @Description: 课程变动监听器
 * @Version: 1.0
 */
@Slf4j
@Component
public class LessonChangeListener {

    @Autowired
    private ILearningLessonService learningLessonService;

    /**
     * 1、监听订单支付消息 -> 新增课程到用户的课表(Day2 - 课堂）
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = LEARNING_LESSON_PAY_QUEUE, durable = "true"),
            exchange = @Exchange(name = ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ORDER_PAY_KEY
    ))
    public void listenOrderPayMsg(OrderBasicDTO orderMsg) {

        // 1、消息健壮性判断
        if (ObjectUtil.isEmpty(orderMsg.getOrderId()) || ObjectUtil.isEmpty(orderMsg.getCourseIds()) || ObjectUtil.isEmpty(orderMsg.getUserId())) {
            log.error("订单支付消息格式异常，消息：{}", orderMsg);
            return;
        }

        // 2、添加课程至课表
        log.info("监听用户：{}的订单{}的支付消息，需要添加课程：{}至用户的课表", orderMsg.getUserId(), orderMsg.getOrderId(), orderMsg.getCourseIds());
        learningLessonService.addLessons(orderMsg.getUserId(), orderMsg.getCourseIds());
    }

    /**
     * 1、聆听退款通知
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = LEARNING_LESSON_REFUND_QUEUE, durable = "true"),
            exchange = @Exchange(name = ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ORDER_REFUND_KEY
    ))
    public void listenOrderRefundMsg(OrderBasicDTO orderMsg) {

        // 1、消息健壮性判断
        if (ObjectUtil.isEmpty(orderMsg.getOrderId()) || ObjectUtil.isEmpty(orderMsg.getCourseIds()) || ObjectUtil.isEmpty(orderMsg.getUserId())) {
            log.error("订单退款消息格式异常，消息：{}", orderMsg);
            return;
        }

        // 2、根据 userId 和 courseId  移除课表中的课程
        log.info("监听用户：{}的订单{}的支付消息，需要添加课程：{}至用户的课表", orderMsg.getUserId(), orderMsg.getOrderId(), orderMsg.getCourseIds());
        learningLessonService.deleteLessons(orderMsg.getUserId(), orderMsg.getCourseIds());

    }
}
